//信息提交
$('#submitInfo').click(function () {
    $.ajax({
        url: "/userForm",
        type: 'post',
        data: $('#userForm').serialize()
    }).done(function (data) {
        $('#info').modal('hide');
        $('#resultSet').text(data);
        $('#submitConfig').attr('disabled', true);
        $("#result").modal('show');
    });
});

//文件上传
/*$('#submitFile').click(function () {
    var formData = new FormData($('#uploadFile')[0]);
    $.ajax({
        url: "/uploadFile",
        type: 'post',
        processData: false,  //不处理发送的数据
        contentType: false,  //不设置Content-Type请求头
        data: formData
    }).done(function (data) {
        $('#upload').modal('hide');
        $('#resultSet').text(data);
        $("#result").modal('show');
    });
});*/

//测试脚本上传
$("#files").fileinput({
    uploadUrl: '/uploadFile',
    allowedFileExtensions : ['java', 'txt'],
    uploadAsync: false,
    maxFileSize: 1000,
    maxFilesNum: 10
}).on("filebatchuploadsuccess", function (e, data) {
    $("#files").fileinput('clear');
    $('#upload').modal('hide');
    var res = data.response;
    if (res.status != 1) {
        $('#resultSet').html('<form class="form-horizontal" id="config"></form>');
        var option;
        for (var i = 0; i < res.Option.length; i++) {
            option += '<option>' + res.Option[i] + '</option>';
        }
        for (var key in res) {
            if (key !== 'Option') {
                $('#config').append('<fieldset id='
                    + key
                    + '><legend>'
                    + key
                    + '</legend></fieldset>');
                for (var i = 0; i < res[key].length; i++) {
                    var tag = key + i;
                    $('#' + key).append('<div class="row form-group"><label for='
                        + tag
                        + ' class="col-sm-offset-1 col-sm-5 control-label">'
                        + res[key][i]
                        + '</label><div class="col-sm-5"><select class="form-control" id='
                        + tag
                        + ' name='
                        + tag
                        + '>'
                        + option
                        + '</select></div></div>');
                }
            }
        }
        $("#result").modal('show');
        $('#submitConfig').attr('disabled', false);
    }
});

//提交配置信息
$('#submitConfig').click(function () {
    $.ajax({
        url: '/config',
        type: 'post',
        data: $('#config').serialize()
    }).done(function (data) {
        $('#resultSet').text(data);
        $('#submitConfig').attr('disabled', true);
        $("#result").modal('show');
    });
});

//开启测试
$('#testScript').click(function () {
    $.ajax({
        url: '/test',
        type: 'post'
    }).done(function (data) {
        $('#resultSet').text(data);
        $('#submitConfig').attr('disabled', true);
        $("#result").modal('show');
    });
});